package com.fudan.backend.service.impl;

import com.fudan.backend.dao.AccountDao;
import com.fudan.backend.dao.StoreDao;
import com.fudan.backend.dao.StoreOpenRequestDao;
import com.fudan.backend.dao.UserDao;
import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Account;
import com.fudan.backend.pojo.Store;
import com.fudan.backend.pojo.StoreOpenRequest;
import com.fudan.backend.pojo.User;
import com.fudan.backend.pojo.statics.*;
import com.fudan.backend.service.AccountService;
import com.fudan.backend.service.StoreOpenRequestService;
import com.fudan.backend.util.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class StoreOpenRequestServiceImpl implements StoreOpenRequestService {

    @Autowired
    StoreOpenRequestDao storeOpenRequestDao;

    @Autowired
    StoreDao storeDao;

    @Autowired
    AccountService accountService;

    @Autowired
    UserDao userDao;

    @Autowired
    Snowflake snowflake;
    @Autowired
    AccountDao accountDao;


    @Override
    @Transactional
    public RetCode addStoreOpenRequest(StoreOpenRequest storeOpenRequest) {

        Date date = new Date();
        storeOpenRequest.setStoreOpenRequestId((snowflake.nextId()).toString());
        storeOpenRequest.setInitiateTime(date);
        storeOpenRequest.setProcessTime(null);
        storeOpenRequest.setStatus(RequestStatus.APPLYING);
        User user = userDao.selectUserByUserId(storeOpenRequest.getUserId());
        if(user == null){
            throw new BaseException(RetCode.USER_ID_NOT_FOUND);
        }
        accountService.transfer(user.getAccountId(), AccountStatic.INTERMEDIATE_ACCOUNT_ID, storeOpenRequest.getRegisteredCapital(), BillType.STORE_OPEN_REQUEST, "注册资金转账");

        int i = storeOpenRequestDao.insertStoreOpenRequest(storeOpenRequest);
        if (i == 1) {
            return RetCode.STORE_OPEN_REQUEST_SUCCESS;
        } else {
            return RetCode.STORE_OPEN_REQUEST_INSERT_FAILURE;
        }
    }

    @Override
    public PageInfo<StoreOpenRequest> getAllStoreOpenRequest(int current, int pageSize) {
        PageHelper.startPage(current, pageSize);
        List<StoreOpenRequest> storeOpenRequests = storeOpenRequestDao.selectAllStoreOpenRequest();
        return new PageInfo<StoreOpenRequest>(storeOpenRequests, 5);
    }

    @Override
    public StoreOpenRequest getStoreOpenRequestByStoreOpenRequestId(String storeOpenRequestId) {
        StoreOpenRequest storeOpenRequest = storeOpenRequestDao.selectStoreOpenRequestByStoreOpenRequestId(storeOpenRequestId);
        if (storeOpenRequest == null) {
            throw new BaseException(RetCode.STORE_OPEN_REQUEST_NOT_FOUND.getCode(), RetCode.STORE_OPEN_REQUEST_NOT_FOUND.getMessage());
        }
        return storeOpenRequest;
    }

    @Override
    @Transactional
    public RetCode processStoreOpenRequest(StoreOpenRequest storeOpenRequest) {

        Date date = new Date();
        storeOpenRequest.setProcessTime(date);

        int updateStoreOpenRequest = storeOpenRequestDao.updateStoreOpenRequest(storeOpenRequest);
        if (updateStoreOpenRequest == 0) {
            throw new BaseException(RetCode.STORE_OPEN_REQUEST_UPDATE_FAILURE.getCode(), RetCode.STORE_OPEN_REQUEST_UPDATE_FAILURE.getMessage());
        }

        Integer status = storeOpenRequest.getStatus();
        String userId = storeOpenRequest.getUserId();
        String accountId = userDao.selectAccountIdByUserId(userId);

        if (status == RequestStatus.APPROVED) {
            Store store = new Store();
            store.setStoreId((snowflake.nextId()).toString());
            store.setUserId(storeOpenRequest.getUserId());
            store.setStoreName(storeOpenRequest.getStoreName());
            store.setCategory(storeOpenRequest.getCategory());
            store.setIdCardNumber(storeOpenRequest.getIdCardNumber());
            store.setStoreProfile(storeOpenRequest.getStoreProfile());
            store.setFilingAddress(storeOpenRequest.getFilingAddress());
            store.setRegisteredCapital(storeOpenRequest.getRegisteredCapital());
            store.setRegistrationDate(storeOpenRequest.getProcessTime());
            store.setPictures(storeOpenRequest.getPictures());
            store.setActivityId(InActivity.NOT_PARTICIPATE_IN_ANY_ACTIVITY);
            store.setAccountId((snowflake.nextId()).toString());
            store.setStatus(StoreStatus.OPEN);
            int insertStore = storeDao.insertStore(store);
            if (insertStore == 0) {
                throw new BaseException(RetCode.STORE_INSERT_FAILURE.getCode(), RetCode.STORE_INSERT_FAILURE.getMessage());
            }
            //商户转账,转到商城利润账户
            Double registeredCapital = storeOpenRequest.getRegisteredCapital();
            accountService.transfer(AccountStatic.INTERMEDIATE_ACCOUNT_ID, AccountStatic.WEBSHOP_ACCOUNT_ID, registeredCapital, BillType.STORE_OPEN_APPROVE, "开店通过转账");
            //创建商店账户
            accountDao.insertAccount(new Account(store.getAccountId()));
        }
        else if(status.equals(RequestStatus.NOT_APPROVED)){
            //转回商户的账户
            accountService.transfer(AccountStatic.INTERMEDIATE_ACCOUNT_ID, accountId, storeOpenRequest.getRegisteredCapital(), BillType.STORE_OPEN_DENY, "开店申请拒绝退款");
        }
        return RetCode.STORE_OPEN_REQUEST_SUCCESS;
    }

    @Override
    public PageInfo<StoreOpenRequest> getAllStoreOpenRequestByUserId(int current, int pageSize, String userId) {
        PageHelper.startPage(current, pageSize);
        List<StoreOpenRequest> storeOpenRequests = storeOpenRequestDao.selectAllStoreOpenRequestByUserId(userId);
        return new PageInfo<StoreOpenRequest>(storeOpenRequests, 5);
    }
}
